package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: zjz
 * Date: 2022/07/24 14:23
 * Description:
 * Version: V1.0
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal表，执行insert操作
        this.save(setmealDto);

        //从dto里面拿到关联关系实体类
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //给每个关系实体类的setmealId赋值
        setmealDishes.stream().map((item) ->{
                item.setSetmealId(setmealDto.getId());
                return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联关系，操作setmeal_dish，执行insert操作
        setmealDishService.saveBatch(setmealDishes);




    }

    @Override
    @Transactional
    public SetmealDto getByIdWithDish(Long setmealId) {
        //先从setmeal表中查出setmeal
        Setmeal setmeal = this.getById(setmealId);
        SetmealDto setmealDto = new SetmealDto();
        //Bean拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询条件构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());

        //从setmeal_dish表中查出对应的套餐-菜品关联关系
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    /**
     * 删除套餐的时候，把setmeal_dish对应关系也删掉
     * 只有停售状态的套餐，才可以删除
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);
        //如果不能删除，就抛出异常信息即可
        if (count > 0 ){
            throw new CustomException("套餐正在售卖中，不能删除！");
        }

        //如果可以删除，先删除套餐表中的数据，setmeal表
        this.removeByIds(ids);

        // 再删除关系表中的数据，setmeal_dish表
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //构造条件
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据，setmeal_dish表
        setmealDishService.remove(lambdaQueryWrapper);

    }
}
